#!/bin/python 
#-*- encoding: utf-8 -*-
#import urllib
import urllib2
import urlparse
import bs4
import json
import chardet
import codecs
import socket

BASE_URL = 'http://liansai.500.com/team/%s/'
TIME_OUT=10
snum=2000
enum=7000
fh=codecs.open('teamurl.csv','a','utf-8')

for num in range(snum,enum):
	try:
		urlstring= BASE_URL % str(num)
		html = urllib2.urlopen(urlstring,'',timeout=TIME_OUT).read()
		soup = bs4.BeautifulSoup(html,from_encoding="gb18030")
		teamname=soup.find('h2','lsnav_qdnav_name')
		if teamname=='' or teamname.string is None:
			continue
		print teamname.string
		fh.write(teamname.string + ',' + urlstring + '\n')
	except urllib2.HTTPError,e:
		print e.code
		num-=1
		continue
	except urllib2.URLError,e:
		num-=1
		continue
	except socket.timeout, e: 
		num-=1
		continue
	except NameError:
		print 'var   not   defined'
		continue

fh.close()
